define("kanas/widget/form/ColorInputBox",
	["dojo/i18n","dojo/text!../resources/ColorInputBox.html","dojo/_base/declare","dojo/_base/kernel","dojo/_base/loader",
	 "dijit/_Widget","dijit/_Templated","dijit/ColorPalette","dijit/form/Button","dojo/i18n!../nls/Widget"],
	function(i18n,template,declare){
		dojo.requireLocalization("kanas.widget", "Widget");
		return declare([i18n,template],{
			
			/** the template path */
			templateString:template,
		
			/** the input DOM node */
	        popupInput: null,
	        input:null,
	        name:"kanas_color_input_box",
	        value:"",
	        colorPicker:null,
	        palette:"7x10",
	        widgetsInTemplate: true,
	        _showPopup:false,
	        enable:true,
	        
	        postCreate:function(){
	        	var colorFlag=false;
	        	this._frameNode=dojo.query("span[class='dijitInline kidMouseHand kidWidgetIcons kidWidgetIconColorPickerEnable']",this.domNode)[0];
	        	this._iconNode=dojo.query("span[class='dijitInline kidWidgetIcons']",this.domNode)[0];
	        	dojo.style(this._iconNode,"color",this.value);
	        	if(this.value=="")
	        		colorFlag=false;
	        	else if(dojo.style(this._iconNode,"color")!="rgb(0, 0, 0)")
	        		colorFlag=true;
	        	else{
	            	dojo.style(this._iconNode,"color","#abcdef");
	            	dojo.style(this._iconNode,"color",this.value);
	            	if(dojo.style(this._iconNode,"color")!="rgb(171, 205, 239)")
	            		colorFlag=true;
	        	}
	        	if(colorFlag){
	        		dojo.style(this._iconNode,"backgroundColor",this.value);
	        		dojo.removeClass(this._iconNode,"kidWidgetIconColorPickerNone");
	        	}
	        	else{
	        		dojo.style(this._iconNode,"backgroundColor","transparent");
	        		dojo.addClass(this._iconNode,"kidWidgetIconColorPickerNone");
	        	}
	        	this.input=dojo.query("input[type='hidden']",this.domNode)[0];
	        	dojo.attr(this.input,"value",this.value);
	        	this.popupInput=dojo.query("input[type='text']",this.domNode)[0];
	        	this.popupSandbox=dojo.query("span.kidcolorsandbox",this.domNode)[0];
	        	var widgets=dojo.query("[widgetId]", this.domNode);
	        	this.popupPane=dijit.byId(dojo.attr(widgets[0],"widgetId"));
	        	var wijit;
	        	for(var i=0,l=widgets.length;i<l;i++){
	        		wijit=dijit.byId(dojo.attr(widgets[i],"widgetId"));
	        		if(wijit)wijit.belongTo=this;
	        	}
	        	var rbWidget = i18n.getLocalization("kanas.widget", "Widget");
	        	dijit.byId(dojo.attr(widgets[1],"widgetId")).set("label",rbWidget.cancel);
	        	dijit.byId(dojo.attr(widgets[2],"widgetId")).set("label",rbWidget.ok);
	        	this.setEnable(this.enable);
	        	this._popupCancel();
	        },
	        _onclick:function(){
	        	if((!this._showPopup)&&this.enable){
		        	dojo.attr(this.popupInput,"value",this.value);
		        	dojo.style(this.popupSandbox,"backgroundColor",this.value);
		        	dijit.popup.open({parent:this, popup:this.popupPane, around: this.domNode});
		        	this._showPopup=true;
	        	}else
	        		this._popupCancel();
	        },
	        _buttonNoneClick:function(){
	        	dojo.style(this.popupSandbox,"backgroundColor","transparent");
	    		dojo.attr(this.popupInput,"value","");
	    		this._popupConfirm();
	        },
	        _popupConfirm:function(){
	        	this.value=dojo.attr(this.popupInput,"value");
	        	var colorFlag=false;
	        	dojo.style(this._iconNode,"color",this.value);
	        	if(this.value=="")
	        		colorFlag=false;
	        	else if(dojo.style(this._iconNode,"color")!="rgb(0, 0, 0)")
	        		colorFlag=true;
	        	else{
	            	dojo.style(this._iconNode,"color","#abcdef");
	            	dojo.style(this._iconNode,"color",this.value);
	            	if(dojo.style(this._iconNode,"color")!="rgb(171, 205, 239)")
	            		colorFlag=true;
	        	}
	        	if(colorFlag){
	        		dojo.style(this._iconNode,"backgroundColor",this.value);
	        		dojo.removeClass(this._iconNode,"kidWidgetIconColorPickerNone");
	        	}
	        	else{
	        		dojo.style(this._iconNode,"backgroundColor","transparent");
	        		dojo.addClass(this._iconNode,"kidWidgetIconColorPickerNone");
	        		this.value="";
	        	}
	        	dojo.attr(this.input,"value",this.value);
	        	this._popupCancel();
	        },
	        _popupCancel:function(){
	        	dijit.popup.hide(this.popupPane);
	        	this._showPopup=false;
	        },
	        _colorPaletteClick:function(value){
	        	dojo.style(this.popupSandbox,"backgroundColor",value);
	        	dojo.attr(this.popupInput,"value",value);
	        },
	        _textColor:function(){
	        	dojo.style(this.popupSandbox,"backgroundColor","");
	        	dojo.style(this.popupSandbox,"backgroundColor",dojo.attr(this.popupInput,"value"));
	        },
	        _filterColorChar:function(event){
	        	event=dojo.fixEvent(event);
	        	if((event.keyCode!=46)&&(event.keyCode!=8)&&(event.keyCode!=37)
	        			&&(event.keyCode!=39)&&(event.keyCode!=16)&&(event.keyCode!=20)
	        			&&(event.keyCode!=20))
	        	if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=65&&event.keyCode<=70)))
	        		{event.returnValue=false;}
	        },
	        setEnable:function(enable){
	        	if(enable){
	        		dojo.addClass(this._frameNode,"kidWidgetIconColorPickerEnable");
	        		dojo.removeClass(this._frameNode,"kidWidgetIconColorPickerDisable");
	        		this.enable=true;
	        	}else{
	        		dojo.removeClass(this._frameNode,"kidWidgetIconColorPickerEnable");
	        		dojo.addClass(this._frameNode,"kidWidgetIconColorPickerDisable");
	        		this.enable=false;
	        	}
	        }
		});
	}
);